package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.ExcursionAvailabilityMgr;
import ejb.contentManagement.ExcursionMgr;
import ejb.dto.ExcursionAvailabilityDTO;
import ejb.dto.ExcursionDTO;

@ManagedBean
@RequestScoped
public class ExcursionAvailabilityBean {
	
	public ExcursionAvailabilityBean() {
		excursionAvailability = new ExcursionAvailabilityDTO();
		excursionAvailability.setExcursionId(excursionId);
		setErrorMessage(null);
	}
	
	@EJB
	private ExcursionAvailabilityMgr excursionAvailabilityMgr; 
	
	@EJB
	private ExcursionMgr excursionMgr;
	
	@SuppressWarnings("unused")
	private List<ExcursionAvailabilityDTO> allExcursionAvailabilities;
	private int excursionId;
	private int excursionAvailabilityId;
	@SuppressWarnings("unused")
	private ExcursionDTO excursion;
	private ExcursionAvailabilityDTO excursionAvailability;
	private String errorMessage;
	
	/**
	 * Delete ExcursionAvailability from database
	 * @param id
	 * @return redirect to page <code>manageExcursionAvailability</code>
	 */
	public String deleteExcursionAvailability(int id) {
		boolean result = excursionAvailabilityMgr.deleteExcursionAvailability(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageExcursionAvailability?faces-redirect=true&excursionId=" + excursionId;
	}
	
	/**
	 * Update ExcursionAvailability in database
	 * @return redirect to page <code>manageExcursionAvailability</code>
	 */
	public String updateExcursionAvailability() {
		excursionAvailability.setExcursionId(excursionId);
		boolean result = excursionAvailabilityMgr.updateExcursionAvailability(excursionAvailability);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageExcursionAvailability?faces-redirect=true&excursionId=" + excursionId;
	}
	
	/**
	 * Insert ExcursionAvailability in database
	 * @return redirect to page <code>manageExcursionAvailability</code>
	 */
	public String insertExcursionAvailability() {
		excursionAvailability.setExcursionId(excursionId);
		boolean result = excursionAvailabilityMgr.insertExcursionAvailability(excursionAvailability);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageExcursionAvailability?faces-redirect=true&excursionId=" + excursionId;
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<ExcursionAvailabilityDTO> getAllExcursionAvailabilities() {
		return excursionAvailabilityMgr.getAllExcursionAvailabilitiesDTO(excursionId);
	}

	public void setAllExcursionAvailabilities(List<ExcursionAvailabilityDTO> allExcursionAvailabilities) {
		this.allExcursionAvailabilities = allExcursionAvailabilities;
	}


	public int getExcursionId() {
		return excursionId;
	}

	public void setExcursionId(int excursionId) {
		this.excursionId = excursionId;
	}

	public ExcursionDTO getExcursion() {
		return excursionMgr.getExcursionDTO(excursionId);
	}

	public void setExcursion(ExcursionDTO excursion) {
		this.excursion = excursion;
	}
	
	public ExcursionAvailabilityDTO getExcursionAvailability(){
		return this.excursionAvailability;
	}
	
	public void setExcursionAvailability(ExcursionAvailabilityDTO excursionAvailability){
		this.excursionAvailability = excursionAvailability;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getExcursionAvailabilityId() {
		return excursionAvailabilityId;
	}

	public void setExcursionAvailabilityId(int excursionAvailabilityId) {
		this.excursionAvailabilityId = excursionAvailabilityId;
		excursionAvailability = excursionAvailabilityMgr.getExcursionAvailabilityDTO(excursionAvailabilityId);
		if(excursionAvailability == null)
			SessionBean.notFound();
	}	

	
}
